package com.cheng.spider.constant;


import com.cheng.spider.exception.UnsupportedMimeException;

public enum MimeType {
    UN_MATCHES("none", "none"),
    HTML("text", "html"),
    IMAGE("image", "*"),
    VIDEO("video", "*")
    ;
    private String mainType;
    private String subType;
    private String charset;

    private MimeType(String mainType, String subType) {
        this.mainType = mainType;
        this.subType = subType;
    }

    public boolean matches(String mainType, String subType) {
        return this.mainType.matches(mainType)
                &&
                (this.subType.matches(subType) || "*".equals(this.subType.trim()));
    }

    public String getExtendName() {
        if (! subType.equals("*")) {
            return subType;
        }
        switch (this) {
            case HTML: return "html";
            case IMAGE: return "jpg";
            case VIDEO: return "mp4";
            case UN_MATCHES:
            default:
                throw new UnsupportedMimeException(
                        "未初始化MIME类型或失配的MIME类型" + this.mainType + "/" + subType);
        }
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public boolean isText() {
        return "text".equals(mainType);
    }
}
